home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / MacPNGlibs / LIBPNG.TXT < prev    next >
Encoding:
Text File  |  1995-08-20  |  36.8 KB  |  872 lines  |  [TEXT/CWIE]

  1. libpng.txt - a description on how to use and modify libpng
  2.  
  3.    libpng 1.0 beta 2 - version 0.8
  4.    For conditions of distribution and use, see copyright notice in png.h
  5.    Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  6.    August 20, 1995
  7.  
  8. This file describes how to use and modify the PNG reference library
  9. (known as libpng) for your own use.  There are four sections to this
  10. file: reading, writing, modifying, and configuration notes for various
  11. special platforms.  Other then this file, the file example.c is a good
  12. starting point for using the library, as it is heavily commented and
  13. should include everything most people will need.
  14.  
  15. Libpng was written as a companion to the PNG specification, as a
  16. way to reduce the amount of time and effort it takes to support
  17. the PNG file format in application programs.  Most users will not
  18. have to modify the library significantly; advanced users may want
  19. to modify it more.  The library was coded for both users.  All
  20. attempts were made to make it as complete as possible, while
  21. keeping the code easy to understand.  Currently, this library
  22. only supports C.  Support for other languages is being considered.
  23.  
  24. Libpng has been designed to handle multiple sessions at one time,
  25. to be easily modifiable, to be portable to the vast majority of
  26. machines (ANSI, K&R, 16 bit, 32 bit) available, and to be easy to
  27. use.  The ultimate goal of libpng is to promote the acceptance of
  28. the PNG file format in whatever way possible.  While there is still
  29. work to be done (see the todo.txt file), libpng should cover the
  30. majority of the needs of it's users.
  31.  
  32. Libpng uses zlib for its compression and decompression of PNG files.
  33. The zlib compression utility is a general purpose utility that is
  34. useful for more then PNG files, and can be used without libpng for
  35. whatever use you want.  See the documentation delivered with zlib for
  36. more details.
  37.  
  38. Those people who do not need to modify libpng should still read at
  39. least part of the PNG specification.  The most important parts are
  40. the data formats and the chunk descriptions.  Those who will be
  41. making changes to libpng should read the whole specification.
  42.  
  43. The structures:
  44.  
  45. There are two main structures that are important to libpng, png_struct
  46. and png_info.  The first, png_struct, is an internal structure that
  47. will not, for the most part, be used by the general user except as
  48. the first variable passed to every png function call.
  49.  
  50. The png_info structure is designed to provide information about the
  51. png file.  All of it's fields are intended to be examined or modified
  52. by the user.  See png.h for a good description of the png_info fields.
  53.  
  54. And while I'm on the topic, make sure you include the png header file:
  55.  
  56. #include <png.h>
  57.  
  58. Checking PNG files:
  59.  
  60. Libpng provides a simple check to see if a file is a png file.  To
  61. use it, pass in the first 1 to 8 bytes of the file, and it will return
  62. true or false (1 or 0) depending on whether the bytes could be part
  63. of a png file.  Of course, the more bytes you pass in, the greater
  64. the accuracy of the prediction.
  65.  
  66.    fread(header, 1, number, fp);
  67.    is_png = png_check_sig(header, number);
  68.  
  69. Reading PNG files:
  70.  
  71. The first thing you need to do while reading a PNG file is to allocate
  72. and initialize png_struct and png_info.  As these are both large, you
  73. may not want to store these on the stack, unless you have stack space
  74. to spare.  Of course, you will want to check if malloc returns NULL.
  75.  
  76.    png_struct *png_ptr = malloc(sizeof (png_struct));
  77.    if (!png_ptr)
  78.       return;
  79.    png_info *info_ptr = malloc(sizeof (png_info));
  80.    if (!info_ptr)
  81.    {
  82.       free(png_ptr);
  83.       return;
  84.    }
  85.  
  86. You may also want to do any i/o initialization here, before
  87. you get into libpng, so if it doesn't work, you don't have
  88. much to undo.
  89.  
  90.    FILE *fp = fopen(file_name, "rb");
  91.    if (!fp)
  92.    {
  93.       free(png_ptr);
  94.       free(info_ptr);
  95.       return;
  96.    }
  97.  
  98. After you have these structures, you will need to set up the
  99. error handling.  When libpng encounters an error, it expects to
  100. longjmp back to your routine.  Therefore, you will need to call
  101. setjmp and pass the jmpbuf field of your png_struct.  If you
  102. read the file from different routines, you will need to update
  103. the jmpbuf field every time you enter a new routine that will
  104. call a png_ function.  See your documentation of setjmp/longjmp
  105. for your compiler for more information on setjmp/longjmp.  See
  106. the discussion on png error handling in the Customizing Libpng
  107. section below for more information on the png error handling.
  108. If an error occurs, and libpng longjmp's back to your setjmp,
  109. you will want to call png_read_destroy() to free any memory.
  110.  
  111.    if (setjmp(png_ptr->jmpbuf))
  112.    {
  113.       png_read_destroy(png_ptr, info_ptr, (png_info *)0);
  114.       /* free pointers before returning, if necessary */
  115.       free(png_ptr);
  116.       free(info_ptr);
  117.       fclose(fp);
  118.       return;
  119.    }
  120.  
  121. Next, you will need to call png_read_init() and png_info_init().
  122. These functions make sure all the fields are initialized to useful
  123. values, and, in the case of png_read_init(), and allocate any memory
  124. needed for internal uses.  You must call png_info_init() first, as
  125. png_read_init() could do a longjmp, and if the info is not initialized,
  126. the png_read_destroy() could try to png_free() random addresses, which
  127. would be bad.
  128.  
  129.    png_info_init(info_ptr);
  130.    png_read_init(png_ptr);
  131.  
  132. Now you need to set up the input code.  The default for libpng is
  133. to use the C function fread().  If you use this, you will need to
  134. pass a valid FILE * in the function png_init_io().  Be sure that
  135. the file is opened in binary mode.  If you wish to handle reading
  136. data in another way, see the discussion on png i/o handling in the
  137. Customizing Libpng section below.
  138.  
  139.    png_init_io(png_ptr, fp);
  140.  
  141. You are now ready to read all the file information up to the actual
  142. image data.  You do this with a call to png_read_info().
  143.  
  144.    png_read_info(png_ptr, info_ptr);
  145.  
  146. The png_info structure is now filled in with all the data necessary
  147. to read the file.  Some of the more important parts of the png_info are:
  148.    width - holds the width of the file
  149.    height - holds the height of the file
  150.    bit_depth - holds the bit depth of one of the image channels
  151.    color_type - describes the channels and what they mean
  152.       see the PNG_COLOR_TYPE_ macros for more information
  153.    channels - number of channels of info for the color type
  154.    pixel_depth - bits per pixel
  155.    rowbytes - number of bytes needed to hold a row
  156.    interlace_type - currently 0 for none, 1 for interlaced
  157.    valid - this details which optional chunks were found in the file
  158.       to see if a chunk was present, OR valid with the appropriate
  159.       PNG_INFO_<chunk name> define.
  160.    palette and num_palette - the palette for the file
  161.    gamma - the gamma the file is written at
  162.    sig_bit and sig_bit_number - the number of significant bits
  163.    trans, trans_values, and number_trans - transparency info
  164.    hist - histogram of palette
  165.    text and num_text - text comments in the file.
  166. for more information, see the png_info definition in png.h and the
  167. PNG specification for chunk contents.  Be careful with trusting
  168. rowbytes, as some of the transformations could increase the space
  169. needed to hold a row (expand, rgbx, xrgb, graph_to_rgb, etc.).
  170.  
  171. A quick word about text and num_text.  PNG stores comments in
  172. keyword/text pairs, one pair per chunk.  While there are
  173. suggested keywords, there is no requirement to restrict the use
  174. to these strings.  There is a requirement to have at least one
  175. character for a keyword.  It is strongly suggested that keywords
  176. be sensible to humans (that's the point), so don't use abbreviations.
  177. See the png specification for more details.  There is no requirement
  178. to have text after the keyword on tEXt chunks.  However, you must
  179. have text after the keyword on zTXt chunks, as only the text gets
  180. compressed, and compressing nothing will result in an error.
  181.  
  182. There is no maximum length on the keyword, and nothing
  183. prevents you from duplicating the keyword.  The text field is an
  184. array of png_text structures, each holding pointer to a keyword
  185. and a pointer to a text string.  Only the text string may be null.
  186. The keyword/text pairs are put into the array in the order that
  187. they are received.  However, some or all of the text chunks may be
  188. after the image, so to make sure you have read all the text chunks,
  189. don't mess with these until after you read the stuff after the image.
  190. This will be mentioned again below in the discussion that goes with
  191. png_read_end().
  192.  
  193. After you've read the file information, you can set up the library to
  194. handle any special transformations of the image data.  The various
  195. ways to transform the data will be described in the order that they
  196. occur.  This is important, as some of these change the color type
  197. and bit depth of the data, and some others only work on certain
  198. color types and bit depths.  Even though each transformation should
  199. check to see if it has data that it can do somthing with, you should
  200. make sure to only enable a transformation if it will be valid for
  201. the data.  For example, don't swap red and blue on grayscale data.
  202.  
  203. This transforms bit depths of less then 8 to 8 bits, changes paletted
  204. images to rgb, and adds an alpha channel if there is transparency
  205. information in a tRNS chunk.  This is probably most useful on grayscale
  206. images with bit depths of 2 or 4 and tRNS chunks.
  207.  
  208.    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  209.       info_ptr->bit_depth < 8)
  210.       png_set_expand(png_ptr);
  211.  
  212.    if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  213.       info_ptr->bit_depth < 8)
  214.       png_set_expand(png_ptr);
  215.  
  216.    if (info_ptr->valid & PNG_INFO_tRNS)
  217.       png_set_expand(png_ptr);
  218.  
  219. This handles alpha and transparency by replacing it with a background
  220. value.  If there was a valid one in the file, you can use it if you
  221. want.  However, you can replace it with your own if you want also.  If
  222. there wasn't one in the file, you must supply a color.  If libpng is
  223. doing gamma correction, you will need to tell libpng where the
  224. background came from so it can do the appropriate gamma correction.
  225. If you are modifying the color data with png_set_expand(), you must
  226. indicate whether the background needs to be expanded.  See the
  227. function definition in png.h for more details.
  228.  
  229.    png_color_16 my_background;
  230.  
  231.    if (info_ptr->valid & PNG_INFO_bKGD)
  232.       png_set_backgrond(png_ptr, &(info_ptr->background),
  233.          PNG_GAMMA_FILE, 1, 1.0);
  234.    else
  235.       png_set_background(png_ptr, &my_background,
  236.          PNG_GAMMA_SCREEN, 0, 1.0);
  237.  
  238. This handles gamma transformations of the data.  Pass both the file
  239. gamma and the desired screen gamma.  If the file does not have a
  240. gamma value, you can pass one anyway if you wish.  Note that file
  241. gammas are inverted from screen gammas.  See the discussions on
  242. gamma in the PNG specification for more information.
  243.  
  244.    if (info_ptr->valid & PNG_INFO_gAMA)
  245.       png_set_gamma(png_ptr, screen_gamma, info_ptr->gamma);
  246.    else
  247.       png_set_gamma(png_ptr, screen_gamma, 0.45);
  248.  
  249. PNG can have files with 16 bits per channel.  If you only can handle
  250. 8 bits per channel, this will strip the pixels down to 8 bit.
  251.  
  252.    if (info_ptr->bit_depth == 16)
  253.       png_set_strip_16(png_ptr);
  254.  
  255. If you need to reduce an rgb file to a paletted file, or if a
  256. paletted file has more entries then will fit on your screen, this
  257. function will do that.  Note that this is a simple match dither, that
  258. merely finds the closest color available.  This should work fairly
  259. well with optimized palettes, and fairly badly with linear color
  260. cubes.  If you pass a palette that is larger then maximum_colors,
  261. the file will reduce the number of colors in the palette so it
  262. will fit into maximum_colors.  If there is an histogram, it will
  263. use it to make intelligent choises when reducing the palette.  If
  264. there is no histogram, it may not do a good job.
  265.  
  266.    if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  267.    {
  268.       if (info_ptr->valid & PNG_INFO_PLTE)
  269.          png_set_dither(png_ptr, info_ptr->palette,
  270.             info_ptr->num_palette, max_screen_colors,
  271.                info_ptr->histogram);
  272.       else
  273.       {
  274.          png_color std_color_cube[MAX_SCREEN_COLORS] =
  275.             { ... colors ... };
  276.  
  277.          png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
  278.             MAX_SCREEN_COLORS, NULL);
  279.       }
  280.    }
  281.  
  282. PNG files describe monocrome as black is zero and white is one.  If you
  283. want this reversed (black is one and white is zero), call this:
  284.  
  285.    if (info_ptr->bit_depth == 1 &&
  286.       info_ptr->color_type == PNG_COLOR_GRAY)
  287.       png_set_invert(png_ptr);
  288.  
  289. PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  However,
  290. they also provide a way to describe the true bit depth of the image.
  291. Then they require bits to be scaled to full range for the bit depth
  292. used in the file.  If you want to reduce your pixels back down to
  293. the true bit depth, call this:
  294.  
  295.    if (info_ptr->valid & PNG_INFO_sBIT)
  296.       png_set_shift(png_ptr, &(info_ptr->sig_bit));
  297.  
  298. PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
  299. they can, resulting in, for example, 8 pixels per byte for 1 bit files.
  300. If you would rather these were expanded to 1 pixel per byte without
  301. changing the values of the pixels, call this:
  302.  
  303.    if (info_ptr->bit_depth < 8)
  304.       png_set_packing(png_ptr);
  305.  
  306. PNG files store 3 color pixels in red, green, blue order.  If you would
  307. rather have the pixels as blue, green, red, call this.
  308.  
  309.    if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  310.       info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  311.       png_set_bgr(png_ptr);
  312.  
  313. For some uses, you may want a grayscale image to be represented as
  314. rgb.  If you need this, call this:
  315.  
  316.    if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
  317.       info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  318.       png_set_gray_to_rgb(png_ptr);
  319.  
  320. PNG files store 16 bit pixels in network byte order (most significant
  321. bit first).  If you would rather store them the other way, (the way
  322. PC's store them, for example), call this:
  323.  
  324.    if (info_ptr->bit_depth == 16)
  325.       png_set_swap(png_ptr);
  326.  
  327. PNG files store rgb pixels packed into 3 bytes.  If you would rather
  328. pack them into 4 bytes, call this:
  329.  
  330.    if (info_ptr->bit_depth == 8 &&
  331.       info_ptr->color_type == PNG_COLOR_TYPE_RGB)
  332.       png_set_filler(png_ptr, filler_byte, PNG_FILLER_BEFORE);
  333.  
  334. where filler_byte is the number to fill with, and the location is
  335. either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
  336. you want the filler before the rgb or after.
  337.  
  338. Finally, if you need the interlacing as discussed below, call
  339. this here:
  340.  
  341.    if (info_ptr->interlace_type)
  342.       number_passes = png_set_interlace_handling(png_ptr);
  343.  
  344. After setting the transformations, you can update your palette by
  345. calling png_start_read_image().  This function is provided for those
  346. who need an updated palette before they read the image data.  If you
  347. don't call this function, the library will automatically call it
  348. before it reads the first row.
  349.  
  350.    png_start_read_image(png_ptr);
  351.  
  352. If you want, libpng will update your png_info structure to reflect
  353. any transformations you've requested with this call.  This is most
  354. useful to update the info structures rowbytes field, so you can
  355. use it to allocate your image memory.  This function calls
  356. png_start_read_image(), so you don't have to call both of them.
  357.  
  358.    png_read_update_info(png_ptr, info_ptr);
  359.  
  360. After you call png_read_update_info(), you can allocate any
  361. memory you need to hold the image.  As the actual allocation
  362. varies among applications, no example will be given.  If you
  363. are allocating one large chunk, you may find it useful to
  364. build an array of pointers to each row, as it will be needed
  365. for some of the functions below.
  366.  
  367. After you've allocated memory, you can read the image data.
  368. The simplest way to do this is in one function call.  If you are
  369. allocating enough memory to hold the whole image, you can just
  370. call png_read_image() and libpng will read in all the image data
  371. and put it in the memory area supplied.  You will need to pass in
  372. an array of pointers to each row.
  373.  
  374. This function automatically handles interlacing, so you don't need
  375. to call png_set_interlace_handling() or call this function multiple
  376. times, or any of that other stuff necessary with png_read_rows().
  377.  
  378.    png_read_image(png_ptr, row_pointers);
  379.  
  380. where row_pointers is:
  381.  
  382.    void *row_pointers[height];
  383.  
  384. You can point to void or char or whatever you use for pixels.
  385.  
  386. If you don't want to read the whole image in at once, you can
  387. use png_read_rows() instead.  If there is no interlacing (check
  388. info_ptr->interlace_type), this is simple:
  389.  
  390.    png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
  391.  
  392. row_pointers is the same as in the png_read_image() call.
  393.  
  394. If you are just calling one row at a time, you can do this for
  395. row_pointers:
  396.  
  397.    char *row_pointers = row;
  398.  
  399.    png_read_rows(png_ptr, &row_pointers, NULL, 1);
  400.  
  401. When the file is interlaced (info_ptr->interlace_type == 1), things
  402. get a good deal harder.  PNG files have a complicated interlace scheme
  403. that breaks down an image into seven smaller images of varying size.
  404. Libpng will fill out those images if you want, or it will give them
  405. to you "as is".  If you want to fill them out, there is two ways
  406. to do that.  The one mentioned in the PNG specification is to expand
  407. each pixel to cover those pixels that have not been read yet.  This
  408. results in a blocky image for the first pass, which gradually smooths
  409. out as more pixels are read.  The other method is the "sparkle" method,
  410. where pixels are draw only in their final locations, with the rest of
  411. the image remaining whatever colors they were initialized to before
  412. the start of the read.  The first method usually looks better, but
  413. tends to be slower, as there are more pixels to put in the rows.  Some
  414. examples to help clear this up:
  415.  
  416. If you don't want libpng to handle the interlacing details, just
  417. call png_read_rows() the correct number of times to read in all
  418. seven images.  See the PNG specification for more details on the
  419. interlacing scheme.
  420.  
  421. If you want libpng to expand the images, call this above:
  422.  
  423.    if (info_ptr->interlace_type)
  424.       number_passes = png_set_interlace_handling(png_ptr);
  425.  
  426. This will return the number of passes needed.  Currently, this
  427. is seven, but may change if another interlace type is added.
  428. This function can be called even if the file is not interlaced,
  429. when it will return one.
  430.  
  431. If you are not going to display the image after each pass, but are
  432. going to wait until the entire image is read in, use the sparkle
  433. effect.  This effect is faster and the end result of either method
  434. is exactly the same.  If you are planning on displaying the image
  435. after each pass, the rectangle effect is generally considered the
  436. better looking one.
  437.  
  438. If you only want the "sparkle" effect, just call png_read_rows() as
  439. normal, with the third parameter NULL.  Make sure you make pass over
  440. the image number_passes times, and you don't change the data in the
  441. rows between calls.  You can change the locations of the data, just
  442. not the data.  Each pass only writes the pixels appropriate for that
  443. pass, and assumes the data from previous passes is still valid.
  444.  
  445.    png_read_rows(png_ptr, row_pointers, NULL, number_of_rows);
  446.  
  447. If you only want the first effect (the rectangles), do the same as
  448. before except pass the row buffer in the third parameter, and leave
  449. the second parameter NULL.
  450.  
  451.    png_read_rows(png_ptr, NULL, row_pointers, number_of_rows);
  452.  
  453. After you are finished reading the image, you can finish reading
  454. the file.  If you are interested in comments or time, you should
  455. pass the png_info pointer from the png_read_info() call.  If you
  456. are not interested, you can pass NULL.
  457.  
  458.    png_read_end(png_ptr, info_ptr);
  459.  
  460. When you are done, you can free all memory used by libpng like this:
  461.  
  462.    png_read_destroy(png_ptr, info_ptr, (png_info *)0);
  463.  
  464. After that, you can discard the structures, or reuse them another
  465. read or write.  For a more compact example of reading a PNG image,
  466. see the file example.c.
  467.  
  468.  
  469. Writing PNG files:
  470.  
  471. Much of this is very similar to reading.  However, everything of
  472. importance is repeated here, so you don't have to constantly look
  473. back up in the Reading PNG files section to understand writing.
  474.  
  475. The first thing you need to do while writing a PNG file is to allocate
  476. and initialize png_struct and png_info.  As these are both large, you
  477. may not want to store these on the stack, unless you have stack space
  478. to spare.
  479.  
  480.    png_struct *png_ptr = malloc(sizeof (png_struct));
  481.    if (!png_ptr)
  482.       return;
  483.    png_info *info_ptr = malloc(sizeof (png_info));
  484.    if (!info_ptr)
  485.    {
  486.       free(png_ptr);
  487.       return;
  488.    }
  489.  
  490. You may also want to do any i/o initialization here, before
  491. you get into libpng, so if it doesn't work, you don't have
  492. much to undo.
  493.  
  494.    FILE *fp = fopen(file_name, "wb");
  495.    if (!fp)
  496.    {
  497.       free(png_ptr);
  498.       free(info_ptr);
  499.       return;
  500.    }
  501.  
  502. After you have these structures, you will need to set up the
  503. error handling.  When libpng encounters an error, it expects to
  504. longjmp back to your routine.  Therefore, you will need to call
  505. setjmp and pass the jmpbuf field of your png_struct.  If you
  506. write the file from different routines, you will need to update
  507. the jmpbuf field every time you enter a new routine that will
  508. call a png_ function.  See your documentation of setjmp/longjmp
  509. for your compiler for more information on setjmp/longjmp.  See
  510. the discussion on png error handling in the Customizing Libpng
  511. section below for more information on the png error handling.
  512.  
  513.    if (setjmp(png_ptr->jmpbuf))
  514.    {
  515.       png_write_destroy(png_ptr);
  516.       /* free pointers before returning.  Make sure you clean up
  517.          anything else you've done. */
  518.       free(png_ptr);
  519.       free(info_ptr);
  520.       fclose(fp);
  521.       return;
  522.    }
  523.  
  524. Next, you will need to call png_write_init() and png_info_init().
  525. These functions make sure all the fields are initialized to useful
  526. values, and, in the case of png_write_init(), allocate any memory
  527. needed for internal uses.  Do png_info_init() first, so if
  528. png_write_init() longjmps, you know info_ptr is valid, so you
  529. don't free random memory pointers, which would be bad.
  530.  
  531.    png_info_init(info_ptr);
  532.    png_write_init(png_ptr);
  533.  
  534. Now you need to set up the input code.  The default for libpng is
  535. to use the C function fwrite().  If you use this, you will need to
  536. pass a valid FILE * in the function png_init_io().  Be sure that
  537. the file is opened in binary mode.  If you wish to handle writing
  538. data in another way, see the discussion on png i/o handling in the
  539. Customizing Libpng section below.
  540.  
  541.    png_init_io(png_ptr, fp);
  542.  
  543. You now have the option of modifying how the compression library
  544. will run.  The following functions are mainly for testing, but
  545. may be useful in certain special cases, like if you need to
  546. write png files extremely fast and are willing to give up some
  547. compression, or if you want to get the maximum possible compression
  548. at the expense of slower writing.  If you have no special needs
  549. in this area, let the library do what it wants, as it has been
  550. carefully tuned to deliver the best speed/compression ratio.
  551. See the compression library for more details.
  552.  
  553.    /* turn on or off filtering (1 or 0) */
  554.    png_set_filtering(png_struct *png_ptr, 1);
  555.  
  556.    png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
  557.    png_set_compression_mem_level(png_ptr, 8);
  558.    png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
  559.    png_set_compression_window_bits(png_ptr, 15);
  560.    png_set_compression_method(png_ptr, 8);
  561.  
  562. You now need to fill in the png_info structure with all the data
  563. you wish to write before the actual image.  Note that the only thing
  564. you are allowed to write after the image is the text chunks and the
  565. time chunk.  See png_write_end() for more information on that.  If you
  566. wish to write them before the image, fill them in now.  If you want to
  567. wait until after the data, don't fill them until png_write_end().  For
  568. all the fields in png_info, see png.h.  For explinations of what the
  569. fields contain, see the PNG specification.  Some of the more important
  570. parts of the png_info are:
  571.    width - holds the width of the file
  572.    height - holds the height of the file
  573.    bit_depth - holds the bit depth of one of the image channels
  574.    color_type - describes the channels and what they mean
  575.       see the PNG_COLOR_TYPE_ defines for more information
  576.    interlace_type - currently 0 for none, 1 for interlaced
  577.    valid - this describes which optional chunks to write to the
  578.       file.  Note that if you are writing a PNG_COLOR_TYPE_PALETTE
  579.       file, the PLTE chunk is not optional, but must still be marked
  580.       for writing.  To mark chunks for writing, OR valid with the
  581.       appropriate PNG_INFO_<chunk name> define.
  582.    palette and num_palette - the palette for the file
  583.    gamma - the gamma the file is written at
  584.    sig_bit and sig_bit_number - the number of significant bits
  585.    trans, trans_values, and number_trans - transparency info
  586.    hist - histogram of palette
  587.    text and num_text - text comments in the file.
  588.  
  589. A quick word about text and num_text.  text is an array of png_text
  590. structures.  num_text is the number of valid structures in the array.
  591. If you want, you can use max_text to hold the size of the array, but
  592. libpng ignores it for writing (it does use it for reading).  Each
  593. png_text structure holds a keyword-text value, and a compression type.
  594. The compression types have the same valid numbers as the compression
  595. types of the image data.  Currently, the only valid number is zero.
  596. However, you can store text either compressed or uncompressed, unlike
  597. images which always have to be compressed.  So if you don't want the
  598. text compressed, set the compression type to -1.  Until text gets
  599. arount 1000 bytes, it is not worth compressing it.
  600.  
  601. The keyword-text pairs work like this.  Keywords should be short
  602. simple descriptions of what the comment is about.  Some typical
  603. keywords are found in the PNG specification, as is some recomendations
  604. on keywords.  You can repeat keywords in a file.  You can even write
  605. some text before the image and some after.  For example, you may want
  606. to put a description of the image before the image, but leave the
  607. disclaimer until after, so viewers working over modem connections
  608. don't have to wait for the disclaimer to go over the modem before
  609. they start seeing the image.  Finally, keywords should be full
  610. words, not abbreviations.  Keywords can not contain NUL characters,
  611. and should not contain control characters.  Text in general should
  612. not contain control characters.  The keyword must be present, but
  613. you can leave off the text string on non-compressed pairs.
  614. Compressed pairs must have a text string, as only the text string
  615. is compressed anyway, so the compression would be meaningless.
  616.  
  617. PNG supports modification time via the png_time structure.  Two
  618. conversion routines are proved, png_convert_from_time_t() for
  619. time_t and png_convert_from_struct_tm() for struct tm.  The
  620. time_t routine uses gmtime().  You don't have to use either of
  621. these, but if you wish to fill in the png_time structure directly,
  622. you should provide the time in universal time (GMT) if possible
  623. instead of your local time.
  624.  
  625. You are now ready to write all the file information up to the actual
  626. image data.  You do this with a call to png_write_info().
  627.  
  628.    png_write_info(png_ptr, info_ptr);
  629.  
  630. After you've read the file information, you can set up the library to
  631. handle any special transformations of the image data.  The various
  632. ways to transform the data will be described in the order that they
  633. occur.  This is important, as some of these change the color type
  634. and bit depth of the data, and some others only work on certain
  635. color types and bit depths.  Even though each transformation should
  636. check to see if it has data that it can do somthing with, you should
  637. make sure to only enable a transformation if it will be valid for
  638. the data.  For example, don't swap red and blue on grayscale data.
  639.  
  640. PNG files store rgb pixels packed into 3 bytes.  If you would rather
  641. supply the pixels as 4 bytes per pixel, call this:
  642.  
  643.    png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  644.  
  645. where the 0 is not used for writing, and the location is either
  646. PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether you
  647. want the filler before the rgb or after.
  648.  
  649. PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
  650. they can, resulting in, for example, 8 pixels per byte for 1 bit files.
  651. If you would rather supply the data 1 pixel per byte, but with the
  652. values limited to the correct number of bits, call this:
  653.  
  654.    png_set_packing(png_ptr);
  655.  
  656. PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  If your
  657. data is of another bit depth, but is packed into the bytes correctly,
  658. this will scale the values to appear to be the correct bit depth.
  659. Make sure you write a sBIT chunk when you do this, so others, if
  660. they want, can reduce the values down to their true depth.
  661.  
  662.    /* do this before png_write_info() */
  663.    info_ptr->valid |= PNG_INFO_sBIT;
  664.  
  665.    /* note that you can cheat and set all the values of
  666.       sig_bit to true_bit_depth if you want */
  667.    if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  668.    {
  669.       info_ptr->sig_bit.red = true_bit_depth;
  670.       info_ptr->sig_bit.green = true_bit_depth;
  671.       info_ptr->sig_bit.blue = true_bit_depth;
  672.    }
  673.    else
  674.    {
  675.       info_ptr->sig_bit.gray = true_bit_depth;
  676.    }
  677.  
  678.    if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  679.    {
  680.       info_ptr->sig_bit.alpha = true_bit_depth;
  681.    }
  682.  
  683.    png_set_shift(png_ptr, &(info_ptr->sig_bit));
  684.  
  685. PNG files store 16 bit pixels in network byte order (most significant
  686. bit first).  If you would rather supply them the other way, (the way
  687. PC's store them, for example), call this:
  688.  
  689.    png_set_swap(png_ptr);
  690.  
  691. PNG files store 3 color pixels in red, green, blue order.  If you would
  692. rather supply the pixels as blue, green, red, call this.
  693.  
  694.    png_set_bgr(png_ptr);
  695.  
  696. PNG files describe moncrome as black is zero and white is one.  If you
  697. would rather supply the pixels with this reversed (black is one and
  698. white is zero), call this:
  699.  
  700.    png_set_invert(png_ptr);
  701.  
  702. That's it for the transformations.  Now you can write the image data.
  703. The simplest way to do this is in one function call.  If have the
  704. whole image in memory, you can just call png_write_image() and libpng
  705. will write the image.  You will need to pass in an array of pointers to
  706. each row.  This function automatically handles interlacing, so you don't
  707. need to call png_set_interlace_handling() or call this function multiple
  708. times, or any of that other stuff necessary with png_write_rows().
  709.  
  710.    png_write_image(png_ptr, row_pointers);
  711.  
  712. where row_pointers is:
  713.  
  714.    void *row_pointers[height];
  715.  
  716. You can point to void or char or whatever you use for pixels.
  717.  
  718. If you can't want to write the whole image at once, you can
  719. use png_write_rows() instead.  If the file is not interlaced,
  720. this is simple:
  721.  
  722.    png_write_rows(png_ptr, row_pointers, number_of_rows);
  723.  
  724. row_pointers is the same as in the png_write_image() call.
  725.  
  726. If you are just calling one row at a time, you can do this for
  727. row_pointers:
  728.  
  729.    char *row_pointers = row;
  730.  
  731.    png_write_rows(png_ptr, &row_pointers, 1);
  732.  
  733. When the file is interlaced, things can get a good deal harder.
  734. PNG files have a complicated interlace scheme that breaks down an
  735. image into seven smaller images of varying size.  Libpng will
  736. build these images if you want, or you can do them yourself.  If
  737. you want to build them yourself, see the PNG specification for
  738. details of which pixels to write when.
  739.  
  740. If you don't want libpng to handle the interlacing details, just
  741. call png_write_rows() the correct number of times to write all
  742. seven sub-images.
  743.  
  744. If you want libpng to build the sub-images, call this:
  745.  
  746.    number_passes = png_set_interlace_handling(png_ptr);
  747.  
  748. This will return the number of passes needed.  Currently, this
  749. is seven, but may change if another interlace type is added.
  750.  
  751. Then write the image number_passes times.
  752.  
  753.    png_write_rows(png_ptr, row_pointers, number_of_rows);
  754.  
  755. As some of these rows are not used, and thus return immediately,
  756. you may want to read about interlacing in the PNG specification,
  757. and only update the rows that are actually used.
  758.  
  759. After you are finished writing the image, you should finish writing
  760. the file.  If you are interested in writing comments or time, you should
  761. pass the an appropriately filled png_info pointer.  If you
  762. are not interested, you can pass NULL.  Be careful that you don't
  763. write the same text or time chunks here as you did in png_write_info().
  764.  
  765.    png_write_end(png_ptr, info_ptr);
  766.  
  767. When you are done, you can free all memory used by libpng like this:
  768.  
  769.    png_write_destroy(png_ptr);
  770.  
  771. Any data you allocated for png_info, you must free yourself.
  772.  
  773. After that, you can discard the structures, or reuse them another
  774. read or write.  For a more compact example of writing a PNG image,
  775. see the file example.c.
  776.  
  777.  
  778. Customizing libpng:
  779.  
  780. There are two issues here.  The first is changing how libpng does
  781. standard things like memory allocation, input/output, and error handling.
  782. The second deals with more complicated things like adding new chunks,
  783. adding new transformations, and generally changing how libpng works.
  784.  
  785. All of the memory allocation, input/output, and error handling in libpng
  786. goes through the routines in pngstub.c.  The file as plenty of comments
  787. describing each function and how it expects to work, so I will just
  788. summarize here.  See pngstub.c for more details.
  789.  
  790. Memory allocation is done through the functions png_large_malloc(),
  791. png_malloc(), png_realloc(), png_large_free(), and png_free().
  792. These currently just call the standard C functions.  The large
  793. functions must handle exactly 64K, but they don't have to handle
  794. more then that.  If your pointers can't access more then 64K at a
  795. time, you will want to set MAXSEG_64K in zlib.h.
  796.  
  797. Input/Output in libpng is done throught png_read() and png_write(), which
  798. currently just call fread() and fwrite().  The FILE * is stored in
  799. png_struct, and is initialized via png_init_io().  If you wish to change
  800. this, make the appropriate changes in pngstub.c and png.h.  Make sure you
  801. change the function prototype for png_init_io() if you are no longer
  802. using a FILE *.
  803.  
  804. Error handling in libpng is done through png_error() and png_warning().
  805. Errors handled through png_error() are fatal, meaning that png_error()
  806. should never return to it's caller.  Currently, this is handled via
  807. setjmp() and longjmp(), but you could change this to do things like
  808. exit() if you should wish.  Similarly, both png_error() and png_warning()
  809. print a message on stderr, but that can also be changed.  The motivation
  810. behind using setjmp() and longjmp() is the C++ throw and catch exception
  811. handling methods.  This makes the code much easier to write, as there
  812. is no need to check every return code of every function call.  However,
  813. there are some uncertainties about the status of local variables after
  814. a longjmp, so the user may want to be careful about doing anything after
  815. setjmp returns non zero besides returning itself.  Consult your compiler
  816. documentation for more details.
  817.  
  818. If you need to read or write custom chunks, you will need to get deeper
  819. into the libpng code.  First, read the PNG specification, and have
  820. a first level of understanding of how it works.  Pay particular
  821. attention to the sections that describe chunk names, and look
  822. at how other chunks were designed, so you can do things similar.
  823. Second, check out the sections of libpng that read and write chunks.
  824. Try to find a chunk that is similar to yours, and copy off of it.
  825. More details can be found in the comments inside the code.
  826.  
  827. If you wish to write your own transformation for the data, look
  828. through the part of the code that does the transformations, and check
  829. out some of the more simple ones to get an idea of how they work.  Try
  830. to find a similar transformation to the one you want to add, and copy
  831. off of it.  More details can be found in the comments inside the code
  832. itself.
  833.  
  834. Configuring for 16 bit platforms:
  835.  
  836. You will probably need to change the png__large_malloc() and
  837. png_large_free() routines in pngstub.c, as these are requred
  838. to allocate 64K.  Also, you will want to look into zconf.h to tell
  839. zlib (and thus libpng) that it cannot allocate more then 64K at a
  840. time.  Even if you can, the memory won't be accessable.  So limit zlib
  841. and libpng to 64K by defining MAXSEG_64K.
  842.  
  843. Configuring for gui/windowing platforms:
  844.  
  845. You will need to change the error message display in png_error() and
  846. png_warning() to display a message instead of fprinting it to stderr.
  847. You may want to write a single function to do this and call it something
  848. like png_message().  On some compliers, you may have to change the
  849. memory allocators (png_malloc, etc.).
  850.  
  851. Configuring for compiler xxx:
  852.  
  853. All includes for libpng are in png.h.  If you need to add/change/delete
  854. an include, this is the place to do it.  The includes that are not
  855. needed outside libpng are protected by the PNG_INTERNAL definition,
  856. which is only defined for those routines inside libpng itself.  The
  857. files in libpng proper only include png.h.
  858.  
  859. Removing unwanted object code:
  860.  
  861. There are a bunch of #define's in png.h that control what parts of
  862. libpng are compiled.  All the defines end in _SUPPORT.  If you are
  863. not using an ability, you can change the #define to #undef and
  864. save yourself code and data space.  All the reading and writing
  865. specific code are in seperate files, so the linker should only grab
  866. the files it needs.  However, if you want to make sure, or if you
  867. are building a stand alone library, all the reading files start with
  868. pngr and all the writing files start with pngw.  The files that
  869. don't match either (like png.c, pngtrans.c, etc.) are used for
  870. both reading and writing, and always need to be included.
  871.  
  872.